package com.webside.front.search.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.webside.front.common.model.BSeries;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.index.service.IndexService;
import com.webside.front.listener.BaseData;
import com.webside.front.search.service.SearchService;
import com.webside.mongo.model.WordRecord;
import com.webside.util.Common;
import com.webside.util.PageUtil;

@Controller
@RequestMapping(value = "/public")
public class SearchController {
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private SearchService searchService;
	@Autowired
	private IndexService indexService;

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
	
	
	@RequestMapping(value="/search.html", method={RequestMethod.GET,RequestMethod.POST})
	public String find(Model model,String code,String type,String name){
		BSeries bSeries=new BSeries();
		//页面标题等信息
		model.addAttribute("bdictionarySPageInfo", searchService.selectPageInfo());
		//查询总数
		model.addAttribute("totalPageCount", searchService.getProductsCount(bSeries));
		//查询 
		model.addAttribute("baseImgPath",BaseData.img_base_path);
		//关键词
		 List<Bdictionary> keywords=indexService.selectKeywords();
		  String []keyarr=keywords.get(0).getName().split(",");
		  model.addAttribute("listKeywords", keyarr);
		//电影
		model.addAttribute("listType", searchService.selectTypeName(type));
		//电视剧
		model.addAttribute("listTVPLAY", searchService.selectTVPLAY(type));
		//动漫
		model.addAttribute("listCARTOON", searchService.selectCARTOON(type));
		
		//点赞查询
		model.addAttribute("listLikeCount", searchService.selectLikeCount());
		
		
		model.addAttribute("address_list",searchService.findAddress());
		
		model.addAttribute("channels_list",searchService.findChannels());
		
		model.addAttribute("year_list",searchService.findYear());
		
		if (code==null) {
			code=searchService.findChannels().get(0).getCode();
		}
		
		
		model.addAttribute("type_list",searchService.findBdictionariesByCode(code));
		model.addAttribute("type",type);
		try {
			if(name!=null){
				model.addAttribute("name", new String(name.getBytes("ISO-8859-1"),"utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Common.BACKGROUND_PATH+"/front/choose";
	}
	
	@RequestMapping(value="/findTypes.html", method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody List<Bdictionary>  findTypesByCode(String code){
		if (code==null) {
			code=searchService.findChannels().get(0).getCode();
		}
		return searchService.findBdictionariesByCode(code);
	}
	@RequestMapping(value="/searchBSeries.html", method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody List<BSeries>searchByPage(Model model,BSeries bSeries, String sort,String channelName,String areaName,String videoCName,HttpServletRequest request){
		System.out.println("_______________");
		System.out.println(JSON.toJSON(bSeries));
		String ip=Common.toIpAddr(request);
		String date=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
		if(bSeries.getName()!=null && bSeries.getName()!=""){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(bSeries.getName());
			wordRecord.setSearchTime(date);
			wordRecord.setIp(Common.toIpAddr(request));
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		
		if(channelName!=null && !channelName.equals("") && !channelName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(channelName);
			wordRecord.setSearchTime(date);
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(areaName!=null && !areaName.equals("") && !areaName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(areaName);
			wordRecord.setSearchTime(date);
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(bSeries.getTypeName()!=null && !bSeries.getTypeName().equals("") && !bSeries.getTypeName().equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(bSeries.getTypeName());
			wordRecord.setSearchTime(date);
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(bSeries.getYear()!=null && !bSeries.getYear().equals("") && !bSeries.getYear().equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(bSeries.getYear());
			wordRecord.setSearchTime(date);
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(videoCName!=null && !videoCName.equals("") && !videoCName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(videoCName);
			wordRecord.setSearchTime(date);
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		model.addAttribute("baseImgPath",BaseData.img_base_path);
		PageUtil pageUtil =new PageUtil();
		pageUtil.setPageNum(1);
		pageUtil.setPageSize(5);
		
		int count=searchService.getProductsCount(bSeries);
		System.out.println(count);
		int totalPageCount;
		if(count>7 ){
			totalPageCount=count/7;
			 if(count%7==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="create_time";
		}
		model.addAttribute("baseImgPath",BaseData.img_base_path);
		System.out.println(totalPageCount);
		List<BSeries> list=searchService.findBSeriesByPageVo(bSeries, pageUtil, sort);
		if(list.size()<20){
			model.addAttribute("totalPageCount", list.size());
		}
		System.out.println(list.size()+"列表大小");
		return list;
	}
	
	@RequestMapping(value="/searchPage.html", method={RequestMethod.GET,RequestMethod.POST})
	public String searchPage(Model model,BSeries bSeries, String pSize,String pageNo,String sort,String channelName,String areaName,String videoCName,HttpServletRequest request){
		model.addAttribute("baseImgPath",BaseData.img_base_path);
		System.out.println("大小"+pSize);
		System.out.println("页码"+pageNo);
		System.out.println("关键词"+sort);
		PageUtil pageUtil =new PageUtil();
		int no=Integer.parseInt(pageNo)+1;
		pageUtil.setPageNum(no);
		pageUtil.setPageSize(Integer.parseInt(pSize));
		int totalPageCount;
		int count=searchService.getProductsCount(bSeries);
		if(count>7 ){
			totalPageCount=count/7;
			 if(count%7==0){
				 model.addAttribute("totalPageCount", totalPageCount);
				  
				}else{
					model.addAttribute("totalPageCount", totalPageCount+1);}
		}else{
			totalPageCount=0;
			model.addAttribute("totalPageCount", totalPageCount);
		}
		if(sort==null){
			sort="create_time";
		}
		String ip=Common.toIpAddr(request);
		if(channelName!=null && !channelName.equals("") && !channelName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(channelName);
			wordRecord.setSearchTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(areaName!=null && !areaName.equals("") && !areaName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(areaName);
			wordRecord.setSearchTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(bSeries.getTypeName()!=null && !bSeries.getTypeName().equals("") && !bSeries.getTypeName().equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(bSeries.getTypeName());
			wordRecord.setSearchTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(bSeries.getYear()!=null && !bSeries.getYear().equals("") && !bSeries.getYear().equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(bSeries.getYear());
			wordRecord.setSearchTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		if(videoCName!=null && !videoCName.equals("") && !videoCName.equals("全部")){
			WordRecord wordRecord=new WordRecord();
			wordRecord.setSearchWord(videoCName);
			wordRecord.setSearchTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
			wordRecord.setIp(ip);
			// 调用java自带的uuid生成唯一标示id
			UUID uuid = UUID.randomUUID();
			// 去掉不需要的字符
			String id = uuid.toString().replaceAll("\\-", "");
			// 为实体对象加入标示id
			wordRecord.setId(id);
			mongoTemplate.insert(wordRecord, "search_word_records");
		}
		List<BSeries> list=searchService.findBSeriesByPageVo(bSeries, pageUtil, sort);
		model.addAttribute("serieslist", list);
		System.out.println(list.size());
		System.out.println(pageUtil.getPageNum()+"||"+pageUtil.getPageSize()+"controller");
		return Common.BACKGROUND_PATH+"/front/serieslist";
	}
	
}
